﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AD.Model;
using AD.BLL;

public partial class Manager_User_PageData_SystemSetting : WebManagerForm
{
    public List<PresentChargeMod> Shops = new List<PresentChargeMod>();
    public int currPageIndex = 1;
    public int MaxSize = 0;
    public int MaxPage = 0;
    public string userName = "";
    public string keyWords = "";
    public string adTitle = "";
    public string endTime = "";
    public SystemSettingMod SystemSetting = new SystemSettingMod();

    protected void Page_Load(object sender, EventArgs e)
    {
        string indexstr = Request.QueryString["pageIndex"];
        string action = Request.QueryString["action"];
        string actions = Request["actions"];
        string idstr = Request.QueryString["id"];
        string idstrs = Request.Params["ids"];
        string filter = "";
        if (!string.IsNullOrEmpty(actions))
        {
            if (actions.Equals("update"))
            {
                //SystemSettingMod system = GetSystemSetting();
                //UpdateInfo(system);
                //SystemSettingBLL.Update(system);
                SystemSettingMod tmp1 = UpdateInfo();
                if (SystemSettingBLL.Update(tmp1) > 0)
                    HttpDataCache.RemoveAppSettings();
                
            }
        }
        if (!string.IsNullOrEmpty(action))
        {
            int id = 0;
            int.TryParse(idstr, out id);
            bool effect = int.TryParse(indexstr, out currPageIndex);
            if (action.Equals("queryFilter") && effect == true)
            {
                SetDataBind(filter, UtilHepler.PAGE_SIZE, 1);
            }
            else if (action.Equals("delete") && id > 0)
            {
                PresentChargeBLL.Delete(id);
                SetDataBind(filter, UtilHepler.PAGE_SIZE, currPageIndex);
            }
            else if (action.Equals("deleteAll"))
            {
                List<int> ids = GetIdToList(idstrs);
                foreach (var tmpId in ids)
                {
                   PresentChargeBLL.Delete(tmpId);
                }
                SetDataBind(filter, UtilHepler.PAGE_SIZE, 1);
            }
            else if (action.Equals("gopage") && effect == true)
            {
                SetDataBind(filter, UtilHepler.PAGE_SIZE, currPageIndex);
            }
        }
        SystemSettingMod tmp = HttpDataCache.AppSettings;
        if (tmp != null) SystemSetting = tmp;
    }

    void SetDataBind(string filter, int pageSize, int index)
    {
        Shops = PresentChargeBLL.QueryByPage(filter, pageSize, index == 0 ? 0 : index - 1, out MaxSize);
        MaxPage = MaxSize / UtilHepler.PAGE_SIZE;
        if (MaxSize % UtilHepler.PAGE_SIZE != 0)
            MaxPage += 1;
        if (Shops == null)
            Shops = new List<PresentChargeMod>();
    }

    //void UpdateInfo(SystemSettingMod tmp)
    SystemSettingMod UpdateInfo()
    {
        SystemSettingMod tmp = new SystemSettingMod();
        tmp.SettingId = HttpDataCache.AppSettings.SettingId;
        tmp.IsEverydayChargeback = Request.Params["IsEverydayChargeback"].Equals("0") ? false : true;
        tmp.IsValidReward = Request.Params["IsValidReward"].Equals("0") ? false : true;
        tmp.EverydayKeyWordBalancePoint = int.Parse(Request.Params["EverydayKeyWordBalancePoint"]);
        tmp.EverydayShopBalancePoint = int.Parse(Request.Params["EverydayShopBalancePoint"]);
        tmp.RewardBalancePoint = int.Parse(Request.Params["RewardBalancePoint"]);
        tmp.IsChangeAudit = Request.Params["IsChangeAudit"].Equals("0") ? false : true;
        tmp.IsAddAudit = Request.Params["IsAddAudit"].Equals("0") ? false : true;
        tmp.MoneyScale = int.Parse(Request.Params["MoneyScale"]);
        tmp.LowPoint = int.Parse(Request.Params["LowPoint"]);
        //HttpDataCache.SystemSetting.IsInMoneyAudit = Request.Params["IsInMoneyAudit"].Equals("0") ? false : true;
        //tmp.IsChangeAudit = Request.Params["IsChangeAudit"].Equals("0") ? false : true;
        //tmp.IsEverydayChargeback = Request.Params["IsEverydayChargeback"].Equals("0") ? false : true;
        //tmp.IsValidReward = Request.Params["IsValidReward"].Equals("0") ? false : true;
        //tmp.EverydayKeyWordBalancePoint = int.Parse(Request.Params["EverydayKeyWordBalancePoint"]);
        //tmp.RewardBalancePoint = int.Parse(Request.Params["RewardBalancePoint"]);
        return tmp;
    }
}